$(function (){
    $('#top').load('/front/top.html')
    $('#foot').load('/front/foot.html')

    //获取新闻
    $.get({
        url:'http://localhost:8080/newsType/find',
        dataType: 'json',
        success(res) {
            if(res.data.length>0){
                $.get({
                    url:'http://localhost:8080/news/find',
                    data:{'newsTypeID':res.data[0].id},
                    dataType:'json',
                    success(res){
                        let count =0;
                        for (let i = 0; i < res.data.length && count<3; i++) {
                            count++;
                            $('#title-one').html(res.data[i].title);
                            $('#content-one').html(res.data[i].content)
                            $('#title-two').html(res.data[i].title);
                            $('#content-two').html(res.data[i].content)
                            $('#title-three').html(res.data[i].title);
                            $('#content-three').html(res.data[i].content)
                        }
                    }
                })
            }
        }
    })
//    获取私教
    $.get({
        url:'http://localhost:8080/teacher/find',
        dataType:'json',
        success(res){
            let count =0;
            if(res.data.length>3){
                $('#teacher').empty();
                for (let i = 0; i <res.data.length&&count<3 ; i++) {
                    count++;
                    let div='<div class="justify-content-center flex-wrap">\n' +
                        '                                    <img src="'+res.data[i].imageLocation+'">\n' +
                        '                                    <div>'+res.data[i].teacherName+'</div>\n' +
                        '                                    <div>'+res.data[i].level+'</div>\n' +
                        '                                </div>'
                    $('#teacher').append(div)
                }
            }
        }
    })
})

function sendContact(){
    let userName=$('#userName').val();
    let phone=$('#phone').val();
    let email=$('#email').val();
    let contact=$('#content').val();

    if(userName===''||phone===''||email===''||contact===''){
        alert('亲，你有选项没输入哦！');
    }else if(!(/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(phone))){
        alert('亲，手机号不规范哦~');
    }else if(!(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test(email))){
        alert('亲，邮箱是我们联系你的重要方式，请填写正确的邮箱哦~')
    }else{
        let data={
            "userName": userName,
            "phone": phone,
            "email": email,
            "content": contact
        }

        $.post({
            url:'http://localhost:8080/contact/add',
            data: data,
            dataType:'json',
            success(res){
                if(res.code>0)
                    alert('出现问题了呢~请稍后重试哦^-^')
                else
                    alert('发送成功，请耐心等待通知哦~')
            }
        })
    }
}